<template>
	<view class="page">
		<view class="q_title">{{ques_title}}</view>
		<view class="qs_box">
			<view class="qs_box_list" v-for="(item,index) in data" :key="index">
				<view class="problem">{{index+1}}、{{item.question}}</view>
				<view class="answer">
					<radio-group class="uni-list" :id="item.id" @change="radioChange">
						<label class="uni-list-cell uni-list-cell-pd" v-for="(item1,index1) in item.answer" :key="index1">
							<view>
								<radio :id="item.id" :value="item1.num" :checked="item1.checked"></radio>
							</view>
							<view style="position:absolute; left:100upx;">
								<label class="label-2-text" :for="item.id">
									<text>{{item1.num}} {{item1.answer}}</text>
								</label>
							</view>
						</label>
					</radio-group>
				</view>
			</view>
		</view>
		<button class="tj" @click="updata" v-show="stu==0">提交</button>
	</view>
</template>

<script>
	import API from '../../common/api.js';
	export default {
		data() {
			return {
				userid:uni.getStorageSync('userid'),
				ques_id:'',
				ques_title:'',
				score:'',//总分合格分数
				num:'',//总题量
				myScore:'',//我已经答题的分数,
				countdone:'',//我已经答题的题量,
				stu:'',//状态 未完成进入，已完成进入
				page:1,
				rows:10,
				data:[],
			}
		},
		onLoad(req){
			this.ques_id=req.id
			this.ques_title=req.title;
			this.score=req.score;
			this.stu=req.stu;//状态 未完成进入，已完成进入
			this.num=req.num;
			this.findProByQues();
		},
		methods: {
			updata(){
				var _this = this;
				if(this.num>this.countdone){//没做完
					uni.showModal({
						content: '请做完所有检测再提交',
						showCancel: false,
					});
				}else if(this.myScore<this.score){//没及格
					uni.showModal({
						content: '检测不合格，是否提交',
						showCancel: false,
						cancelText: '',
						confirmText: '',
						success: res => {
							if(res.confirm){
								uni.request({
									url:API.finishDati,
									method:"GET",
									data:{
										deptid:uni.getStorageSync('deptid'),
										ques_id:_this.ques_id,
									},
									header:API.get_head(),
									success: (res) => {
										uni.navigateBack({
											delta: 1
										});
										uni.showToast({
											title:res.data.message,
										})
									},
									fail: () => {
										uni.showToast({
											title:"加载失败",
											icon:'none'
										})
									}
								})
							}
						},
						fail: () => {
							
						},
					});
				}else{
					uni.request({
						url:API.finishDati,
						method:"GET",
						data:{
							deptid:uni.getStorageSync('deptid'),
							ques_id:_this.ques_id,
						},
						header:API.get_head(),
						success: (res) => {
							uni.navigateBack({
								delta: 1
							});
							uni.showToast({
								title:res.data.message,
							})
						},
						fail: () => {
							uni.showToast({
								title:"加载失败",
								icon:'none'
							})
						}
					})
				}
			},
			findProByQues(){
				var _this = this;
				uni.request({
					url:API.findProByQues,
					method:"GET",
					data:{
						ques_id:_this.ques_id,
						ques_title:_this.ques_title,
						page:_this.page,
						rows:_this.rows
					},
					header:API.get_head(),
					success: (res) => {
						_this.data = res.data;
						for(var i=0;i<_this.data.length;i++){
							_this.data[i].answer = JSON.parse(_this.data[i].answer);
						}
						if(_this.stu==1){//有答案的
							this.datiLog();
						}
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					}
				})
			},
			datiLog(){//我的答案
				var _this = this;
				uni.request({
					url:API.datiLog,
					method:"GET",
					data:{
						deptid:uni.getStorageSync('deptid'),
						ques_id:_this.ques_id,
					},
					header:API.get_head(),
					success: (res) => {
						_this.data;
						for(var i=0;i<_this.data.length;i++){//题目
							for(var j=0;j<res.data.length;i++){//我的答案
								if(_this.data[i].id==res.data[j].pro_id){
									for(var j=0;j<_this.data[i].answer.length;i++){//添加选中
										if(_this.data[i].answer.num == res.data[j].answers){
											_this.data[i].answer.checked = true;
										}else{
											_this.data[i].answer.checked = false;
										}
									}
									break;
								}
							}
						}
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					}
				})
			},
			radioChange: function(e) {
				var _this = this;
				uni.request({
					url:API.api_dati,
					method:"GET",
					data:{
						userid:_this.userid,
						answer:e.detail.value,
						pro_id:e.currentTarget.id,
					},
					header:API.get_head(),
					success: (res) => {
						if(res.data.success){
							_this.myScore = res.data.myScore;
							_this.countdone = res.data.countdone;
						}
					},
					fail: () => {
						uni.showToast({
							title:"加载失败",
							icon:'none'
						})
					}
				})
// 				var checked = e.target.value
// 				var changed = {}
// 				for (var i = 0; i < this.radioItems.length; i++) {
// 					if (checked.indexOf(this.radioItems[i].name) !== -1) {
// 						changed['radioItems[' + i + '].checked'] = true
// 					} else {
// 						changed['radioItems[' + i + '].checked'] = false
// 					}
// 				}
			},
		}
	}
</script>

<style>
	.page{
		height:100%;
		width:100%;
	}
	.q_title{
		font-size:38upx;
		font-weight:bold;
		color:#222222;
		background:#FFFFFF;
		padding:32upx 24upx;
		border-bottom:1upx solid #e5e5e5;
	}
	.qs_box{
		padding:20upx 24upx 118upx;
	}
	.qs_box_list{
		padding:50upx 30upx;
		background:#FFFFFF;
		margin-bottom:20upx;
	}
	.problem{
		font-size: 30upx;
		color:#303030;
	}
	.uni-list{
		padding:0;
	}
	.uni-list:before{display:none;}
	.uni-list:after{display:none;}
	.uni-list-cell{padding:60upx 0 0;border:none;}
	.uni-list-cell:after{display: none;}
	.tj{
		position:fixed;
		bottom: 0;
		left:0;
		width:100%;
		border-radius:0;
		background:#067aee;
		height:98upx;
		color:#FFFFFF;
		font-size:34upx;
	}
</style>
